package com.hrp.service;

import com.hrp.domain.ResponseData;
import com.hrp.domain.TransportRecord;
import com.hrp.domain.UimmsReceiveMoney;
import com.hrp.hrpdao.ReceiveMoneyDao;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2019/5/6 0006.
 */
@Service
public class ReceiveMoneyService {


    private static final Logger logger = LoggerFactory.getLogger(ReceiveMoneyService.class);

    @Autowired
    private ReceiveMoneyDao receiveMoneyDao;

    public Map<String,Object> getReceiveMoneyRecordByPage(Integer page, Integer rows) {

            Map<String, Object> jsonMap = new HashMap<String, Object>();//定义map
            try{
                Integer total = receiveMoneyDao.getTotalCount();
                //List<Jxkhzq> list = jxkhzqDao.getJxkhzqByPage(10,20);

                Integer startIndex = (page-1)*rows;
                Integer endIndex = page*rows;
                List<Integer> listStr = new ArrayList<>();
                for(Integer i=(startIndex+1);i<=endIndex;i++){
                    listStr.add(i);
                }
                List<UimmsReceiveMoney> list = receiveMoneyDao.getReceiveMoneyRecordByPage(listStr);
                jsonMap.put("total", total);//total键 存放总记录数，必须的
                jsonMap.put("rows", list);//rows键 存放每页记录 list
                return jsonMap;
            }catch (Exception e) {
                e.printStackTrace();
                logger.info("获取分页账期数据失败");
                return jsonMap;

            }


    }

    public ResponseData saveReceiveMoneyRecordData(UimmsReceiveMoney uimmsReceiveMoney) {
        try{
            UimmsReceiveMoney uimmsReceiveMoneyOld = receiveMoneyDao.getReceiveMoneyRecordById(uimmsReceiveMoney.getId());
            if(uimmsReceiveMoneyOld==null){
                receiveMoneyDao.saveReceiveMoneyRecord(uimmsReceiveMoney);
            }else{
                receiveMoneyDao.updateReceiveMoneyRecord(uimmsReceiveMoney);
            }
            return ResponseData.build(200,"保存成功");
        }catch (Exception e){
            e.printStackTrace();
            return ResponseData.build(500,"保存失败");
        }
    }

    public Map<String, Object> getReceiveMoneyDataSummary(String sendTimeKsrq, String sendTimeJsrq, String receiverIdStr) {
        Map<String,String> params = new HashMap<>();
        params.put("sendTimeKsrq",""+sendTimeKsrq+"");
        params.put("sendTimeJsrq",""+sendTimeJsrq+"");
        params.put("receiverIdStr",""+receiverIdStr+"");


        Map<String, Object> jsonMap = new HashMap<String, Object>();//定义map
        try{
            List<Map<String,String>>  lists = receiveMoneyDao.getReceiveMoneyDataSummaryForProc(params);

            jsonMap.put("total", lists.get(0).get("totalCount"));//total键 存放总记录数，必须的
            jsonMap.put("rows", lists);//rows键 存放每页记录 list
            return jsonMap;
        }catch (Exception e) {
            e.printStackTrace();
            logger.info("获取分页账期数据失败");
            return jsonMap;
        }
    }
}
